package other.edan.erp.k3;

import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.api.annotations.*;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEvent;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventRequest;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventResponse;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.service.XoqlService;
import other.edan.erp.utils.ResultVo;

/**
 * @Description: 校验变更单是否允许变更
 * @Author: 黎炎松
 * @Date: 2025/3/13 17:41
 */
@RestApi(baseUrl = "/edan/changeOrder")
public class CheckChangeOrder implements ApprovalEvent  {
    private XoqlService xoqlService = XoqlService.instance();
    private static final Logger LOGGER = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "校验变更单是否允许变更 ==> ";

    /**
     * 审批事件
     * @param request
     * @return
     * @throws ScriptBusinessException
     */
    @Override
    public ApprovalEventResponse execute(ApprovalEventRequest request) throws ScriptBusinessException {
        LOGGER.info(LOGGER_MSG + "开始处理");
        // 推送数据
        Long orderId = request.getDataId();
        PushUpOrder pushUpOrder = new PushUpOrder();
        ResultVo resultVo = pushUpOrder.pushUpOrderToK3(orderId, 1);

        ApprovalEventResponse response = new ApprovalEventResponse();
        response.setMsg(resultVo.getMsg());
        response.setSuccess(resultVo.getCode() == 200 ? true : false);
        return response;
    }

    /**
     * 手动触发校验是否允许变更
     * @param orderId 订单id
     * @return
     */
    @RestMapping(value = "/handerIsCanUp", method = RequestMethod.POST)
    public ResultVo handerIsCanUp(@RestQueryParam(name = "orderId") Long orderId) {
        LOGGER.debug("手动触发校验是否允许更新 orderId = " + orderId);
        PushUpOrder pushUpOrder = new PushUpOrder();
        return pushUpOrder.pushUpOrderToK3(orderId, 1);
    }

    /**
     * 新增、编辑页面保存前进行校验
     * @param propData 订单数据
     * @return
     */
    @RestMapping(value = "/pageCheckIsCanUp", method = RequestMethod.POST)
    public ResultVo pageCheckIsCanUp(@RestBeanParam(name = "body") JSONObject propData) {
        LOGGER.debug("页面校验是否允许更新 propData = " + propData);
        return ResultVo.success();
    }

}
